package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.TextureView;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.entity.HomeListEntity;
import com.eightbears.beboy.entity.OrderDetailEntity;
import com.eightbears.beboy.entity.OrderEntity;
import com.eightbears.beboy.entity.PayOrderEntity;
import com.eightbears.beboy.entity.ProductsEntity;
import com.eightbears.beboy.entity.status.Status_ShopDetail;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.eightbears.beboy.entity.WaitPaymentEntity.PARAMS_WAIT_PAYMENT;
import static com.eightbears.beboy.ui.merchant.activity.ShopPaymentActivity.PARAMS_GET_MONEY_2_SHOP;
import static com.eightbears.beboy.ui.user.activity.ByVipActivity.PARAMS_BUY_VIP;
import static com.eightbears.beboy.ui.user.activity.OrderDetailActivity.PARAMS_ORDER_DETAIL;
import static com.eightbears.beboy.ui.user.activity.PaymentActivity.EVENT_MY_PAY;
import static com.eightbears.beboy.ui.user.activity.TransferActivity.PARAMS_PAY_TRANSFER;
import static com.eightbears.beboy.ui.user.fragment.HomeFragment.EVENT_BUY_POST_SUCCESS;
import static com.eightbears.beboy.ui.user.fragment.HomeFragment.EVENT_BUY_VIP_SUCCESS;
import static com.eightbears.beboy.ui.user.fragment.HomeFragment.PARAMS_POST_ITEM;

public class BuyResultActivity extends BaseActivity {

    public static final String EVENT_BUY_SET_MEAL_SUCCESS = "buySetMealSuccess";

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_title_shop)
    TextView tv_title_shop;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_result)
    TextView tv_result;
    @BindView(R.id.tv_view_order)
    TextView tv_view_order;
    @BindView(R.id.tv_price)
    TextView tv_price;

    private HomeListEntity.Post post;
    private ProductsEntity productsEntity;
    private OrderDetailEntity.Order orderDetailEntity;
    private Status_ShopDetail status_shopDetail;
    private OrderEntity.OrderDetailEntity orderDetail;
    private PayOrderEntity.PayOrder waitPaymentEntity;
    private String mTransfer;
    private String mMypay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_result);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

//        mMealdetailBean = (ShopMealListEntity.DataBean.ListBean) getIntent().getExtras().get(PARAMS_MEAL_DETAIL);
        orderDetailEntity = (OrderDetailEntity.Order) getExtras().get(PARAMS_ORDER_DETAIL);
        post = (HomeListEntity.Post) getExtras().get(PARAMS_POST_ITEM);
        productsEntity = (ProductsEntity) getExtras().get(PARAMS_BUY_VIP);
        status_shopDetail = (Status_ShopDetail) getExtras().get(PARAMS_GET_MONEY_2_SHOP);
        waitPaymentEntity = (PayOrderEntity.PayOrder) getExtras().get(PARAMS_WAIT_PAYMENT);
        mTransfer = (String) getExtras().get(PARAMS_PAY_TRANSFER);
        mMypay = (String) getExtras().get(EVENT_MY_PAY);
        tv_date.setText(CommonUtils.TimeStamp2Date(System.currentTimeMillis() + "", "MM-dd HH:mm"));
        tv_title.setText(R.string.text_buy_result);
        if (post != null) {
            initPostDetail();
        }

        if (productsEntity != null) {
            initVIPDetail();
        }

        if (orderDetailEntity != null) {
            initOrderDetail();
        }

        if (status_shopDetail != null) {
            initRepostMoney();
        }

        if (null != orderDetail) {
            initPayOrderDetail();
        }

        if (null != waitPaymentEntity) {
            initWaitPayment();
        }

        if (!TextUtils.isEmpty(mTransfer)) {
            initWaitTransfer();
        }
        if (!TextUtils.isEmpty(mMypay)) {
            initBalance();
        }
    }
    private void initBalance() {
        tv_title_shop.setText(R.string.text_pay_2_shop_success);
        String price = String.format(getString(R.string.text_price), mMypay);
        tv_price.setText(price);
        tv_result.setText(getString(R.string.text_buy_success));
    }


    private void initWaitTransfer() {
        tv_title_shop.setText(R.string.text_pay_2_shop_success);
        String price = String.format(getString(R.string.text_price), mTransfer);
        tv_price.setText(price);
        tv_result.setText(getString(R.string.text_transfer_success));
    }

    private void initRepostMoney() {
        tv_title_shop.setText(R.string.text_pay_2_shop_success);
        String price = String.format(getString(R.string.text_price), status_shopDetail.getData().getRepostMoney());
        tv_price.setText(price);
        tv_result.setText(getString(R.string.text_buy_success));
    }

    private void initOrderDetail() {
        tv_title_shop.setText(orderDetailEntity.getTitle());
        String price = String.format(getString(R.string.text_price), orderDetailEntity.getDiscount_price());
        tv_price.setText(price);
        tv_result.setText(getString(R.string.text_buy_success));
    }

    private void initPayOrderDetail() {
        tv_title_shop.setText(orderDetail.getData().getShop().getTitle());
        String price = String.format(getString(R.string.text_price), orderDetail.getData().getAmount());
        tv_price.setText(price);
        tv_result.setText(getString(R.string.text_buy_success));
    }

    private void initWaitPayment() {
        tv_title_shop.setText(waitPaymentEntity.getObject().getTitle());
        String price = String.format(getString(R.string.text_price), waitPaymentEntity.getReceipt_amount());
        tv_price.setText(price);
        tv_result.setText(getString(R.string.text_buy_success));
    }


    private void initPostDetail() {
        String title = String.format(getString(R.string.text_buy_post_success_title), post.getUser().getNickname());
        tv_result.setText(R.string.text_buy_post_success);
        tv_title_shop.setText(title);
        String price = String.format(getString(R.string.text_price), post.getPrice() == 0 ? post.getRepost().getPrice() + "" : post.getPrice() + "");
        tv_price.setText(price);
    }

    private void initVIPDetail() {
        String title = String.format(getString(R.string.text_buy_vip_success_title), productsEntity.getTitle());
        String price = String.format(getString(R.string.text_price), productsEntity.getMoney());
        tv_result.setText(title);
        tv_price.setText(price);
        tv_title_shop.setText(getString(R.string.text_buy_success));
    }


    @OnClick(R.id.tv_view_order)
    void openOrderList() {
        postEvent();
        finish();
    }

    private void postEvent() {
        if (null != post) {
            EventBus.getDefault().post(EVENT_BUY_POST_SUCCESS);
        }

        if (null != productsEntity) {
            EventBus.getDefault().post(EVENT_BUY_VIP_SUCCESS);
        }

        if (null != waitPaymentEntity) {
            EventBus.getDefault().post(EVENT_BUY_SET_MEAL_SUCCESS);
        }

        if (null != orderDetailEntity) {
            ShowToast.showShortToast(getString(R.string.text_buy_success));
        }

        if (!TextUtils.isEmpty(mTransfer)) {
            EventBus.getDefault().post(PARAMS_PAY_TRANSFER);
        }

        if (!TextUtils.isEmpty(mMypay)) {
            EventBus.getDefault().post(EVENT_BUY_POST_SUCCESS);
            EventBus.getDefault().post(EVENT_MY_PAY);
        }
    }

    @Override
    public void finish() {
        super.finish();
        postEvent();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

}
